package com.skh.stack;

import java.util.Stack;

/**
 * @Author: skh
 * @Date: 2018/10/22 10:11
 * @Description: 堆栈模拟队列
 */
public class StackMockQueue {

    private Stack<Integer> stackA = new Stack<>();
    private Stack<Integer> stackB = new Stack<>();

    //入队
    public void enQueue(Integer element) {
        stackA.push(element);
    }

    //出队
    public Integer deQueue() {
        //判断栈B是否有元素,有则直接出,没有则把栈A中的元素弹出,压入栈B
        if (stackB.isEmpty()) {
            if (stackA.isEmpty()) {
                return null;
            }
            transfer();
        }
        return stackB.pop();

    }

    private void transfer() {
        while (!stackA.isEmpty()) {
            stackB.push(stackA.pop());
        }
    }

    public static void main(String[] args) {
        StackMockQueue stackMockQueue = new StackMockQueue();
        Integer i1 = stackMockQueue.deQueue();
        System.out.println("i1 = " + i1); //null

        stackMockQueue.enQueue(1);
        stackMockQueue.enQueue(2);
        System.out.println(stackMockQueue.deQueue());//1
        System.out.println(stackMockQueue.deQueue());//2
        System.out.println(stackMockQueue.deQueue());//null

    }


}
